package com.lanswon.qzsmk.controller.inventoryManageController;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.annotation.SystemControllerLog;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.InventoryException;
import com.lanswon.qzsmk.service.systemManageService.InventoryManagementService;
import com.lanswon.qzsmk.vo.StorageRegRecVo;

import ch.qos.logback.classic.Logger;

@Controller
@RequestMapping(value = "/inventoryMagOfIncomingCancel")
public class InventoryMagOfIncomingCancellationController {
	private final static Logger logger = (Logger) LoggerFactory.getLogger(InventoryMagOfBlanAndFinishkCardTurnInByBranchController.class);
	
	@Autowired
	InventoryManagementService ims;
	
	/**
	 * 子模块：入库撤销
	 * 跳转入库撤销页面
	 * @return
	 */
	@RequiresPermissions("inventoryMagOfIncomingCancel:index")
	@RequestMapping(name = "入库撤销查询画面,inventoryMagOfIncomingCancel:index",value = "/storageCancellation", method = {RequestMethod.GET,RequestMethod.POST})
	@SystemControllerLog(description = "跳转入库撤销页面")
	public String storageCancellation(HttpServletRequest request) {
		return "StorageCancellation";
	}
	
	/**
	 * 子模块：入库撤销 
	 * 获取入库撤销信息
	 * @param cpv
	 * @param pageCount
	 * @throws Exception
	 */
	@RequiresPermissions("inventoryMagOfIncomingCancel:getStorageCancel")
	@RequestMapping(name = "获取入库信息,inventoryMagOfIncomingCancel:getStorageCancel",value = "/getStorageCancel", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "获取入库撤销信息")
	public Map<String, Object> getStorageCance(HttpServletRequest request, Integer pageNum, Integer numPerPage) throws Exception{
		Map<String, Object> rMap = new HashMap<>();
		if (pageNum == null) {
			pageNum = 1;
		}
		if (numPerPage == null) {
			numPerPage = 10;
		}
		// 接收前画面穿过来数据
		String storageNoAsNum = request.getParameter("storageNo");
		String storageNo = null;
		if(!"".equals(storageNoAsNum)&&storageNoAsNum!=null){
			storageNo = "KC"+request.getParameter("storageNo");
		}
		String storageMode = request.getParameter("storageModeBySC");
		String storageDateFrom = request.getParameter("storageDateFrom");
		String storageDateTo = request.getParameter("storageDateTo");
		String branchNo = request.getParameter("branchNo");
		String employeeNo = request.getParameter("employeeNo");
		 if(storageDateFrom!=null && !storageDateFrom.equals("")){
	        	String date[]=storageDateFrom.split("-");
	    		String dateFlag= date[0]+date[1]+date[2];
	        	storageDateFrom=dateFlag;
	        	if(storageDateTo!=null && !storageDateTo.equals("")){
	        		String dateTo[]=storageDateTo.split("-");
		    		String dateFlag1= dateTo[0]+dateTo[1]+dateTo[2];
		    		storageDateTo=dateFlag1;
	        	}
	        }else if(storageDateTo!=null && !storageDateTo.equals("")){
	        	String date[]=storageDateTo.split("-");
	    		String dateFlag= date[0]+date[1]+date[2];
	    		storageDateTo=dateFlag;
	    		if(storageDateFrom!=null && !storageDateFrom.equals("")){
	        		String dateTo[]=storageDateFrom.split("-");
		    		String dateFlag1= dateTo[0]+dateTo[1]+dateTo[2];
		    		storageDateFrom=dateFlag1;
	        	}
	        }
		// 装有画面信息的map
		Map<String, String> map = new HashMap<String, String>();
		map.put("storageNo", storageNo);
		map.put("storageMode", storageMode);
		map.put("storageDateFrom", storageDateFrom);
		map.put("storageDateTo", storageDateTo);
		map.put("branchNo", branchNo);
		map.put("employeeNo", employeeNo);
		logger.debug("storageDateFrom==>" + storageDateFrom);
		logger.debug("storageDateTo==>" + storageDateTo);

		DwzPage<StorageRegRecVo> srv = ims.queryAllStorageRegs(map, pageNum, numPerPage);
		
		
		rMap.put("srv", srv);
		return rMap;
	}
	
	/**
	 * 子模块：入库撤销 
	 * 处理跳转卡卡片配 送登记页面的参数
	 * @param request
	 * @return
	 * @throws InventoryException 
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("inventoryMagOfIncomingCancel:incomingCanceling")
	@RequestMapping(name = "入库信息提交,inventoryMagOfIncomingCancel:incomingCanceling",value = "/incomingCanceling", method = RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "处理跳转卡片配送登记页面的参数")
	public Map<String, Object> goStorageCanRegister(String StorageCanceled, HttpServletRequest request) throws InventoryException {
		Map<String, Object> map = new HashMap<>();
		// 解析选中的项目,属性名：属性值
		String[] itemArrays = StorageCanceled.split("#");
		// 进行撤销操作
		
		ims.incomingCancelSubmit(itemArrays);
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "inventoryMagOfIncomingCancel/storageCancellation");
		return map;
	}
}
